/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utility;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Jeppe
 */
public class Adgangskodekryptering {

    private String PW, encryptedPW, decryptedPW;

    private static MessageDigest md;
/**
 * 
 * @param cryptedpass
 * @param comparepass
 * @return 
 */
    public static boolean isTheSameMD5(String cryptedpass, String comparepass){
        boolean isTheSame = false;
        if(cryptedpass.equals(cryptWithMD5(comparepass))){
            isTheSame = true;
        }
        return isTheSame;
    }
    
    public static String cryptWithMD5(String pass) {
        try {
            md = MessageDigest.getInstance("MD5");
            byte[] passBytes = pass.getBytes();
            md.reset();
            byte[] digested = md.digest(passBytes);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < digested.length; i++) {
                sb.append(Integer.toHexString(0xff & digested[i]));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException ex) {
            System.out.println("Kunne ikke kryptere password");
            return null;
        }
        
    }
    
    // <editor-fold defaultstate="collapsed" desc="Bruges ikke umiddelbart">     
    public String encrypt(String password) {
//        PW = password;
//        try {
//            String plainData = password, cipherText, decryptedText;
//            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
//            keyGen.init(128);
//            SecretKey secretKey = keyGen.generateKey();
//            Cipher aesCipher = Cipher.getInstance("AES");
//            aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
//            byte[] byteDataToEncrypt = plainData.getBytes();
//            byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
//            cipherText = new BASE64Encoder().encode(byteCipherText);
//            aesCipher.init(Cipher.DECRYPT_MODE, secretKey, aesCipher.getParameters());
//            byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
//            decryptedText = new String(byteDecryptedText);
//            System.out.println("\n Plain Data : " + plainData + " \n Cipher Data : " + cipherText + " \n Decrypted Data : " + decryptedText);
//            encryptedPW = cipherText;
//
//        } catch (NoSuchAlgorithmException ex) {
//            Logger.getLogger(Adgangskodekryptering.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IllegalBlockSizeException ex) {
//            Logger.getLogger(Adgangskodekryptering.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (BadPaddingException ex) {
//            Logger.getLogger(Adgangskodekryptering.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (InvalidKeyException ex) {
//            Logger.getLogger(Adgangskodekryptering.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (InvalidAlgorithmParameterException ex) {
//            Logger.getLogger(Adgangskodekryptering.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (NoSuchPaddingException ex) {
//            Logger.getLogger(Adgangskodekryptering.class.getName()).log(Level.SEVERE, null, ex);
//        }
        return encryptedPW;
    }// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Bruges ikke umiddelbart"> 
    public String decrypt(String encyptedPW) {
//        this.encryptedPW = encyptedPW;
//        String password = null;
//        try {
//            String plainData = password, cipherText, decryptedText;
//            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
//            keyGen.init(128);
//            SecretKey secretKey = keyGen.generateKey();
//            Cipher aesCipher = Cipher.getInstance("AES");
//            aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
//            byte[] byteDataToEncrypt = plainData.getBytes();
//            byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
//            cipherText = new BASE64Encoder().encode(byteCipherText);
//            aesCipher.init(Cipher.DECRYPT_MODE, secretKey, aesCipher.getParameters());
//            byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
//            decryptedText = new String(byteDecryptedText);
//            System.out.println("\n Plain Data : " + plainData + " \n Cipher Data : " + cipherText + " \n Decrypted Data : " + decryptedText);
//            decryptedPW = decryptedText;
//
//        } catch (NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException ex) {
//            Logger.getLogger(Adgangskodekryptering.class.getName()).log(Level.SEVERE, null, ex);
//        }
        return decryptedPW;
    }// </editor-fold>
}
